<template>
  <div class="app-container">
    <div class="search-container">
      <el-form ref="queryFormRef" :model="queryParams" label-width="auto" :inline="true">
        <el-form-item label="供应商" prop="vehicleTypeIds">
          <el-select filterable clearable @keyup.enter="handleQuery" v-model="queryParams.supplierId" placeholder="请选择" style="width: 256px">
            <el-option v-for="item in supplierOptions" :key="item.id" :label="item.companyName" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="城市" prop="cityId">
          <el-select filterable clearable @keyup.enter="handleQuery" v-model="queryParams.cityId" placeholder="请选择" style="width: 256px">
            <el-option v-for="item in cityOptions" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="司机名称" prop="driverName">
          <el-input @keyup.enter="handleQuery" v-model="queryParams.driverName" placeholder="请输入" style="width: 256px"></el-input>
        </el-form-item>
        <el-form-item label="司机电话" prop="driverPhone">
          <el-input @keyup.enter="handleQuery" v-model="queryParams.driverPhone" placeholder="请输入" style="width: 256px"></el-input>
        </el-form-item>
        <el-form-item label="车牌" prop="carNo">
          <el-input @keyup.enter="handleQuery" v-model="queryParams.carNo" placeholder="请输入" style="width: 256px"></el-input>
        </el-form-item>
        <el-form-item label="身份证" prop="idCard">
          <el-input @keyup.enter="handleQuery" v-model="queryParams.idCard" placeholder="请输入" style="width: 256px"></el-input>
        </el-form-item>
        <el-form-item label="协议状态" prop="protocolStatus">
          <el-select @keyup.enter="handleQuery" v-model="queryParams.protocolStatus" placeholder="请选择" style="width: 256px">
            <el-option :value="0">待审核</el-option>
            <el-option :value="1">通过</el-option>
            <el-option :value="2">驳回</el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">
            <template #icon><Search /></template>
            搜索
          </el-button>
          <el-button @click="handleResetQuery">
            <template #icon><Refresh /></template>
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <template #header>
        <!-- <el-button type="success" @click="handleEdit('新增', {})">
          <template #icon><Plus /></template>
          新增
        </el-button> -->
        <el-button type="warning" @click="handleExport">
          <template #icon><Edit /></template>
          导出
        </el-button>
      </template>
      <el-table v-loading="loading" :data="TableData" highlight-current-row row-key="id" @row-click="handleRowClick">
        <el-table-column label="司机id" min-width="120" align="center" prop="id"></el-table-column>
        <el-table-column label="姓名" min-width="120" align="center" prop="driverName"></el-table-column>
        <el-table-column label="电话" min-width="120" align="center" prop="driverPhone"></el-table-column>
        <el-table-column label="性别" min-width="120" align="center" prop="sex">
          <template #default="scope">
            <div v-if="scope.row.sex == 1">男</div>
            <div v-else>女</div>
          </template>
        </el-table-column>
        <el-table-column label="钱包状态" min-width="120" align="center">
          <template #default="scope">
            <div style="color: red" v-if="scope.row.walletStatus == 0">冻结</div>
            <div v-else>正常</div>
          </template>
        </el-table-column>
        <el-table-column label="提现策略" min-width="120" align="center">
          <template #default="scope">
            <div v-if="scope.row.strategy == 1">周结算</div>
            <div v-else>日结算</div>
          </template>
        </el-table-column>
        <el-table-column label="司机状态" min-width="120" align="center">
          <template #default="scope">
            <div v-if="scope.row.status == 1">审核通过</div>
            <div v-else-if="scope.row.status == 2">审核驳回</div>
            <div v-else>待审核</div>
          </template>
        </el-table-column>
        <el-table-column label="协议审核状态" min-width="120" align="center">
          <template #default="scope">
            <div v-if="scope.row.protocolStatus == 0">待审核</div>
            <div v-else-if="scope.row.protocolStatus == 1">通过</div>
            <div v-else>驳回</div>
          </template>
        </el-table-column>
        <el-table-column label="城市名称" min-width="80" align="center" prop="cityName"></el-table-column>
        <el-table-column label="车型" min-width="80" align="center" prop="vehicleType"></el-table-column>
        <el-table-column label="车品牌" min-width="140" align="center" prop="vehicleSeries"></el-table-column>
        <el-table-column label="车辆颜色" min-width="80" align="center" prop="carColour"></el-table-column>
        <el-table-column label="车牌" min-width="140" align="center" prop="carNo"></el-table-column>
        <el-table-column label="能源类型" min-width="80" align="center" prop="powerType"></el-table-column>
        <el-table-column label="准驾车型" min-width="80" align="center" prop="driverLicenseType"></el-table-column>
        <el-table-column label="身份证" min-width="166" align="center" prop="idCard"></el-table-column>
        <el-table-column label="协议审核时间" min-width="80" align="center" prop="protocolAuditTime"></el-table-column>
        <el-table-column label="协议审核人" min-width="80" align="center" prop="protocolAdmin"></el-table-column>
        <el-table-column label="协议备注" min-width="80" align="center" prop="protocolRemark"></el-table-column>
        <el-table-column label="协议生效时间" min-width="100" align="center" prop="protocolTime"></el-table-column>
        <el-table-column fixed="right" align="center" label="操作" width="220">
          <template #default="scope">
            <div style="display: flex; justify-content: center; align-items: center">
              <el-button type="primary" link size="small" @click.stop="handleEdit('编辑', scope.row)">
                <template #icon><Pointer /></template>
                编辑
              </el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-if="total > 0" v-model:total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize" @pagination="handleQuery" />
    </el-card>
    <!-- 改派 -->
    <EditModal ref="editModalRef" @success="handleQuery" />
  </div>
</template>

<script setup lang="ts">
defineOptions({
  name: "SysMenu",
  inheritAttrs: false,
});

import CommonApi from "@/api/common";
import { carTypeArr, xcCarTypeArr, fzCarTypeArr, nhCarTypeArr, hnCarTypeArr, xcCarEnum, fzCarTypeEnum, nhCarTypeEnum, hvCarEnum } from "@/enums/common";
import DriverApi from "@/api/driver";
import EditModal from "./components/editModal.vue";
import ExportApi from "@/api/exports";

const queryFormRef = ref(ElForm);
const editModalRef = ref(null);
const total = ref(0);
const supplierOptions = ref();
const cityOptions = ref();
const useTimes = ref();
const createdTimes = ref();

const loading = ref(false);

// 查询参数
const queryParams = reactive<MenuQuery>({
  pageNo: 1,
  pageSize: 10,
});
// 菜单表格数据
const TableData = ref([]);

// 选择表格的行菜单ID
const selectedMenuId = ref<number | undefined>();

function handleExport() {
  ExportApi.driverExport(queryParams).then((res) => {
    let data = res.data;
    ElMessage.success("导出完毕");
    if (!data) {
      return;
    }
    if (typeof window.navigator.msSaveBlob !== "undefined") {
      window.navigator.msSaveBlob(new Blob([data], { type: "application/vnd.ms-excel" }), fileName + ".xls");
    } else {
      let url = window.URL.createObjectURL(new Blob([data], { type: "application/vnd.ms-excel" }));
      let link = document.createElement("a");
      link.style.display = "none";
      link.href = url;
      link.setAttribute("download", "司机" + ".xlsx");
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link); //下载完成移除元素
      window.URL.revokeObjectURL(url); //释放掉blob对象
    }
  });
}

//编辑触发
function handleEdit(title: string, row: any) {
  editModalRef.value.edit(title, row);
}
// 查询菜单
function handleQuery() {
  loading.value = true;
  DriverApi.getList(queryParams)
    .then((data) => {
      TableData.value = data.result;
      total.value = data.total * 1;
    })
    .finally(() => {
      loading.value = false;
    });
}

function handleUseTimeChange(time: any) {
  if (time) {
    queryParams.useTimeFrom = `${time[0]} 00:00:00`;
    queryParams.useTimeTo = `${time[1]} 23:59:59`;
  } else {
    queryParams.useTimeFrom = undefined;
    queryParams.useTimeTo = undefined;
  }
}
function handleCreatedTimeChange(time: any) {
  if (time) {
    queryParams.createTimeFrom = `${time[0]} 00:00:00`;
    queryParams.createTimeTo = `${time[1]} 23:59:59`;
  } else {
    queryParams.createTimeFrom = undefined;
    queryParams.createTimeFrom = undefined;
  }
}

// 重置查询
function handleResetQuery() {
  queryParams.useTimeFrom = undefined;
  queryParams.useTimeTo = undefined;
  queryParams.createTimeFrom = undefined;
  queryParams.createTimeTo = undefined;
  useTimes.value = null;
  createdTimes.value = null;

  queryFormRef.value.resetFields();
  queryParams.pageNo = 1;
  handleQuery();
}

// 行点击事件
function handleRowClick(row: MenuVO) {
  // 记录表格选择的菜单ID，新增子菜单作为父菜单ID
  selectedMenuId.value = row.id;
}

onMounted(() => {
  handleQuery();
  CommonApi.getSupplier({ pageNo: 1, pageSize: 9999, status: 1 }).then((data) => {
    if (data.code == 0) {
      supplierOptions.value = data.result;
    }
  });
  CommonApi.getCity({ pageNo: 1, pageSize: 9999 }).then((data) => {
    if (data.code == 0) {
      cityOptions.value = data.result;
    }
  });
});
</script>
<style lang="scss" scoped>
.red_rate {
  color: red;
}
.address_box {
  display: flex;
  flex-direction: column;
  align-items: left;
  justify-content: left;
  .item {
    display: flex;
    align-items: center;
    margin: 4px 0;
    .tag {
      width: 50px;
      height: 23px;
      text-align: center;
      color: orange;
      background: #fff8e6;
    }
    .tag2 {
      color: #3fd175;
      background: #e7faf0;
    }
  }
}
</style>
